package cn.edu.bit.snake;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.List;

import cn.edu.bit.snake.Snake.Direction;

public class SnakeClient extends Frame{

	public static final int GAME_WIDTH = 800;
	public static final int GAME_HEIGHT = 600;
	public static final int GAME_LOCATION_X = 300;
	public static final int GAME_LOCATION_Y = 100;
	
	public static int GAME_LEVEL = 1;	
	
	public static final int SNAKE_START_X = 60;
	public static final int SNAKE_START_Y = 60;
	public static final Direction SNAKE_START_DIR = Direction.R;
	public static final int SNAKE_WIDTH = 15;
	public static final int SNAKE_HEIGHT = 15;
	public static final int SNAKE_LENGTH = 3;
	
	Image offScreanImage = null;
	
	Snake s = null;
	Fruit f = null;
	
	public static void main(String[] args) {
		SnakeClient sc = new SnakeClient();
		sc.launch();
	}

	//initiate sth
	public void launch(){
		this.setSize(this.GAME_WIDTH,this.GAME_HEIGHT);
		this.setLocation(this.GAME_LOCATION_X, this.GAME_LOCATION_Y);
		this.setResizable(false);
		this.setBackground(Color.ORANGE);
		this.setLayout(new FlowLayout());
		this.setVisible(true);

		
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
			
		});
		this.addKeyListener(new KeyMonitor());
		
		
		s = new Snake(SNAKE_START_X,SNAKE_START_Y,SNAKE_START_DIR,true,SNAKE_LENGTH,this);
		generateFruit();
		
		//begin another thread to repaint
		SCRunnable scr = new SCRunnable();
		Thread t = new Thread(scr);
		t.start();
		
	}
	
	
	@Override
	public void paint(Graphics g) {
		if (s != null)
			s.draw(g);
		f.draw(g);
	}
	
	
	public class SCRunnable implements Runnable{

		@Override
		public void run() {
			while (true){
				try {
					Thread.sleep(200 - SnakeClient.GAME_LEVEL * 20);
					repaint();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	@Override
	public void update(Graphics g) {
		if (offScreanImage == null){
			offScreanImage = this.createImage(GAME_WIDTH,GAME_HEIGHT);
		}
		
		Graphics gg = offScreanImage.getGraphics();
		Color c = gg.getColor();
		gg.setColor(Color.ORANGE);
		gg.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gg.setColor(c);
		paint(gg);
		g.drawImage(offScreanImage, 0, 0, null);
	}


	public class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_SPACE){
				s = null;
				new MenuDialog().setVisible(true);
			}
			s.keyPressed(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			//s.keyReleased(e);
		}
		
	}
	
	//get a random number
	public int getRandomNum(int numLength){
		int num;
		Random r = new Random();
		num = r.nextInt(numLength);
		return num;
	}
	
	//generate a fruit
	public void generateFruit(){
		int x = getRandomNum(GAME_WIDTH / 15);
		x *= 15;
		int y = getRandomNum(GAME_HEIGHT / 15);
		y *= 15;
		if (y < 30)
			y += 30;
		if (x > 780)
			x -= 15;
		if (y > 585)
			y -= 15;
		f = new Fruit(x,y,this);
		for (int i = 0;i < this.s.sl.size();i++){
			if (this.s.getRectangle(this.s.sl.get(i)).intersects(f.getRectangle(f))){
				f = null;
				generateFruit();
			}
		}
	}
	
	class MenuDialog extends Dialog{
		TextField tf = new TextField("1");
		Button b = new Button("Start");
		
		public MenuDialog(){
			super(SnakeClient.this,true);
			this.setLocation(400,300);
			this.setLayout(new FlowLayout());
			this.add(new Label("Please choose level:"));
			this.add(tf);
			this.add(new Label("1--3"));
			this.add(b);
			this.pack();
			
			this.addWindowListener(new WindowAdapter(){
				@Override
				public void windowClosing(WindowEvent arg0) {
					setVisible(false);
				}
			});
			
			b.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					SnakeClient.GAME_LEVEL = Integer.valueOf(tf.getText().trim());
					setVisible(false);
					s = null;
					s = new Snake(SNAKE_START_X,SNAKE_START_Y,SNAKE_START_DIR,true,SNAKE_LENGTH,SnakeClient.this);
				}
			});
		}
	}
}



